package com.yupi.mianshiya.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.mianshiya.constant.FavoriteConstant;
import com.yupi.mianshiya.mapper.ArticlesMapper;
import com.yupi.mianshiya.mapper.QuestionMapper;
import com.yupi.mianshiya.model.entity.Favorite;

import com.yupi.mianshiya.model.vo.FavoriteVO;
import com.yupi.mianshiya.service.FavoriteService;
import com.yupi.mianshiya.mapper.FavoriteMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class FavoriteServiceImpl extends ServiceImpl<FavoriteMapper, Favorite>
    implements FavoriteService{

    @Resource
    private FavoriteMapper favoriteMapper;

    @Resource
    private ArticlesMapper articlesMapper;

    @Resource
    private QuestionMapper questionMapper;

    @Override
    public boolean isFavorited(Long userId, Favorite favorite) {
        return favoriteMapper.exists(
                new QueryWrapper<Favorite>()
                        .eq("userId", userId)
                        .eq("contentId", favorite.getContentId())
                        .eq("contentType", favorite.getContentType())
        );
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean toggleFavorite(Long userId, Favorite favorite) {
        boolean isFavorited = isFavorited(userId, favorite);

        if (isFavorited) {
            // 取消收藏
            return favoriteMapper.delete(
                    new QueryWrapper<Favorite>()
                            .eq("userId", userId)
                            .eq("contentId", favorite.getContentId())
                            .eq("contentType", favorite.getContentType())
            ) > 0;
        } else {
            // 添加收藏
            Favorite newFavorite = new Favorite();
            newFavorite.setUserId(userId);
            newFavorite.setContentId(favorite.getContentId());
            newFavorite.setContentType(favorite.getContentType());
            return favoriteMapper.insert(newFavorite) > 0;
        }
    }

    @Override
    public Page<FavoriteVO> getFavoriteList(Long userId, int pageSize, int current, String contentType) {
        // 1. 分页查收藏
        QueryWrapper<Favorite> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId);
        queryWrapper.eq(contentType!= null, "contentType", contentType);
        queryWrapper.orderByDesc("createTime");
        Page<Favorite> favoritePage = this.page(new Page<>(current, pageSize), queryWrapper);

        // 2. 组装 VO 列表
        List<FavoriteVO> voList = favoritePage.getRecords().stream()
                .map(record -> {
                    FavoriteVO vo = new FavoriteVO();
                    BeanUtils.copyProperties(record, vo);
                    // 补 content
                    if (FavoriteConstant.Article.equals(record.getContentType())) {
                        vo.setContent(articlesMapper.selectById(record.getContentId()).getTitle());
                    } else if (FavoriteConstant.Question.equals(record.getContentType())) {
                        vo.setContent(questionMapper.selectById(record.getContentId()).getTitle());
                    }
                    return vo;
                })
                .collect(Collectors.toList());

        // 3. 把 VO 列表和分页信息一起返回
        Page<FavoriteVO> voPage = new Page<>(current, pageSize);
        BeanUtils.copyProperties(favoritePage, voPage);
        voPage.setRecords(voList);




        return voPage;
    }



}






